import traceback
from fastapi import FastAPI, Request, Cookie, HTTPException, Form,UploadFile,File,status,Depends
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from dependencies import *
import tracemalloc

from models import *
from api.auth import router as auth_router
from api.upload import router as upload_router
from api.qa import router as qa_router
app = FastAPI()
tracemalloc.start()

# 将SessionMiddleware添加到应用中
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)

# 设置静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")

# 包含 auth 路由
app.include_router(auth_router)
# 包含 upload 路由
app.include_router(upload_router)
app.include_router(qa_router)

@app.get("/")
async def get_home_page(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

@app.middleware("http")
async def catch_exceptions_middleware(request, call_next):
    try:
        return await call_next(request)
    except Exception as e:
        traceback.print_exc()
        raise e

# 启动服务器的命令
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
